<?php

include 'php-ofc-library/open-flash-chart.php';
require_once 'bootstrap.php';

//////////////////////////////start of calling database//////////////////////////

global $cur_time;

$dbstat = new DbStat();
date_default_timezone_set("Asia/Singapore");

// y break down
if(isset($_GET['sp1']))
	$y_value = $_GET['sp1'];
else
	$y_value = 15;
	
	


//creating an array for the past 24hrs breakdown starting from 0-1 hr, 1-2 hr ....
$dbss = array();
$hour = $_GET["hour"];
$tmp = $cur_time+(($hour*$_GET["x"])*60*60);

for($i = 0; $i < 60*$_GET["x"]/$y_value; $i++) {
	$dbss[] = $dbstat->getDuration(date('YmdHis',$tmp+$y_value*$i*60), date('YmdHis', $tmp+$y_value*($i+1)*60));
} 


$averages = array();
foreach ($dbss as $key => $dbs1) {
	$averages[$key] = 0.0; // initialize
	
	if($dbs1) {
		// if $dbs1 is not empty
		foreach($dbs1 as $res) {
			$averages[$key] += $res['PCT_FREE_MEM'];
		}
		
		$averages[$key] /= sizeof($dbs1); // get average
		//var_dump(sizeof($dbs1));
	}
}

$data = array();
for ($i = 0; $i < 60*$_GET["x"]/$y_value; $i++) {
	$data[] = floatval($averages[$i]);
}

//getting thresholds from user config
$urs_config = new UserConfig($_SESSION['user']);
$first = $urs_config->getFirst('PCT_FREE_MEM');
$second = $urs_config->getSecond('PCT_FREE_MEM');
if ($first > $second) {
	$upper = $first;
	$lower = $second;
} else {
	$upper = $second;
	$lower = $first;
}

///////////////////////////////end of calling database//////////////////////////


///// start of graph //////
$title = new title('Percentage of free memory (%)' );

$y = new y_axis(); 
$y->set_range( 0, 100, 10 );
$x = new x_axis(); 
$x->set_range( 0, 60*$_GET["x"]/$y_value, 1);

$x_labels = new x_axis_labels();

$x_label_arr = array();
for ($i = 0; $i < 60*$_GET["x"]/$y_value; $i++) {
	$x_label_arr[] = (string)(($i+1) * $y_value);
}

$x_labels->set_labels(
    $x_label_arr
    );
$x->set_labels($x_labels);

$x_legend = new x_legend( 'no. of mins' );
$x_legend->set_style( '{font-size: 16px; color: #FFFFFF}' );
$y_legend = new y_legend( 'percentage (%)' );
$y_legend->set_style( '{font-size: 16px; color: #FFFFFF}' );

$bar_value_arr = array(); 
foreach($data as $value)
{
	$barValue = new bar_value($value);
	if($value <= $lower ) {
	$barValue->set_colour('#00FF00'); //green
	}
	elseif ($value > $lower && $value <= $upper) {
	$barValue->set_colour('#FFFF00'); //yellow
	}
	else {
	$barValue->set_colour('#FF0000'); //red
	}
	$bar_value_arr[]=  $barValue;
}

$bar = new bar_glass();
$bar->set_values($bar_value_arr );
$bar->key('healthy', 12);
$bar->set_colour('#00FF00');

$empty = new line();
$empty->set_key('not so healthy', 12);
$empty->set_colour('#FFFF00');

$empty1 = new line();
$empty1->set_key('unhealthy', 12);
$empty1->set_colour('#FF0000');

$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $bar );
$chart->add_element( $empty );
$chart->add_element( $empty1 );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_x_legend( $x_legend );
$chart->set_y_legend( $y_legend );
$chart->set_bg_colour( '#736F6E' );


///////////////////////end of graph//////////////////////////////
?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>Database Performance Dashboard</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<script type="text/javascript" src="js/json/json2.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF("open-flash-chart.swf", "thirdlevel_chart", "550", "400", "9.0.0");
</script>
<script type="text/javascript">
function open_flash_chart_data()
{
    return JSON.stringify(data);
}

function findSWF(movieName) {
  if (navigator.appName.indexOf("Microsoft")!= -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}
    
var data = <?php echo $chart->toPrettyString(); ?>;
</script>
</head>
  <body>
    <div id="container">
      <!-- Start of Page Header -->
      <div id="page_header">
          <div id="company_name">
          <h1><span>Oracle Database Performance Dashboard</span></h1>
          </div>
      </div>
      <div id="page_menu">
          <ul>
          <li class="home"><a href="./index.php" title="Home"><span>Home</span></a></li>
          <li class="monitor"><a href="./firstlevel.php" title="First Level of Parameters Monitor"><span>Monitor</span></a></li>
          <li class="debug"><a href="./debug.php" title="Debug Interface"><span>Debug</span></a></li>
          <li class="settings"><a href="./settings.php" title="Settings"><span>Settings</span></a></li>
          <li class="aboutus"><a href="./aboutUs.php" title="About Us"><span>About Us</span></a></li>
          </ul>
      </div>
	  <div id="logout"><a href="./logout.php">logout</a></div>
      <div id="main_content">
          <div class="content_header">
          <h2><span>Database Performance Dashboard</span></h2>
          </div>
          <div class="content_box_right">
          <div class="content_box_left">
          <div class="content_box">
          <div id="main_content_border">
              <div class="topmasters">
                  <div class="thumbnail">
                  <img src="images/_thumbnail02.gif" width="133" height="93" alt="Thumbnail Caption" />
              </div>
              <div class="topmasters_info">
			  <h2>Zoom in to <? echo $_GET["hour"]*$_GET["x"] ?> - <? echo ($_GET["hour"]+1)*$_GET["x"]?>  hour block</h2>
			  <form id="third_sp1" action="thirdlevel_sp1.php" method="get"><label><font color="white">y unit time block: </font></label><input id="sp1" name="sp1" value="<?php if(!isset($_GET['sp1'])) echo '15'; else echo $_GET['sp1'];?>"/> 
			  <input id="x" name="x" type="hidden" value="<?php if(!isset($_GET['x'])) echo '1'; else echo $_GET['x'];?>"/><font color="white">minutes</font><input type="hidden" name="hour" value="<?=$hour?>"/><input type="submit" value="Refresh" /></form>
			  <br />
			  <br />
			 <div id="thirdlevel_chart"></div>
          
                  </div>
                  <div class="clearthis">&nbsp;</div>
              </div>
  
              <!-- End of Top Masters Listing 1 -->
  
          </div>
          </div>
          </div>
          </div>
  
      </div>
  
      <!-- End of Main Content -->
  
  
      <div class="clearthis">&nbsp;</div>
  
  
      <!-- Start of Page Footer -->
  
      <div id="page_footer">
      Design by <a href="aboutUs.php">CS5226 Group</a>
      </div>
  
  </div>
  
  </body>
  </html>